﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    /// <summary>
    /// 会员充值搜索时间
    /// </summary>
    /// <param name="sFilter">过滤条件</param>
    public delegate void EventCustomerPaymentSearch(string sFilter);

    public partial class CustomerPaymentSearchControl : UserControl
    {
        private const string strAll = "所有";

        public EventCustomerPaymentSearch PaymentSearch { get; set; }
        
        public CustomerPaymentSearchControl()
        {
            InitializeComponent();
            InitEmployee();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cbeEmployee.SelectedValue == null)
                return;

            int iEmployeeID = Convert.ToInt32(cbeEmployee.SelectedValue);

            string sFilter = "";
            if (txtName.Tag != null)
            {
                string sCustomerID = txtName.Tag.ToString();
                sFilter = string.Format("{0}={1}", DBEntityCustomInfo.FieldName.CUSTOMERID, sCustomerID);
            }

            if (iEmployeeID != -1)
            {
                string filter = string.Format("{0}={1}", DBEntityCustomerPayment.FieldName.CEMPLOYEEID, iEmployeeID);
                if (string.IsNullOrEmpty(sFilter))
                    sFilter = filter;
                else
                    sFilter += " AND " + filter;
            }

            if (PaymentSearch != null)
            {
                PaymentSearch(sFilter);
            }

        }

        private void InitEmployee()
        {
            string error = string.Empty;
            string filter = string.Empty;
            DataSet ds = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryEmployeeState(DBEntityEmployee.ENUM_ESTATE.F0E, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取经手人信息失败\r\n" + error);
                return;
            }

            DataRow[] drFinds = ds.Tables[0].Select(string.Format("{0}=''", DBEntityEmployee.FieldName.ENAME));
            if (drFinds.Length == 0)
            {
                DataRow drNew = ds.Tables[0].NewRow();
                drNew[DBEntityEmployee.FieldName.EMPLOYEEID] = -1;
                drNew[DBEntityEmployee.FieldName.ENAME] = strAll;
                ds.Tables[0].Rows.InsertAt(drNew, 0);
            }

            cbeEmployee.DataSource = ds.Tables[0].DefaultView;
            cbeEmployee.ValueMember = DBEntityEmployee.FieldName.EMPLOYEEID;
            cbeEmployee.DisplayMember = DBEntityEmployee.FieldName.ENAME;

            cbeEmployee.SelectedIndex = 0;
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            DataSet dsCustomer = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustomer, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员信息失败\r\n" + error);
                return;
            }

            frmCustomQueryList form = new frmCustomQueryList(dsCustomer.Tables[0]);
            if (form.ShowDialog() == DialogResult.OK)
            {
                DataRow dr = form.getCustomInfoRow;
                string sCustomerID = dr[DBEntityCustomInfo.FieldName.CUSTOMERID].ToString();
                string sCustomerName = dr[DBEntityCustomInfo.FieldName.CNAME].ToString();
                txtName.Tag = sCustomerID;
                txtName.Text = sCustomerName;
            }
        }
    }
}
